﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtils.Utils.SpecialUtils.View;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using RabbitDownload.WebSite.WebCurrency;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Bcy
{
    internal class BcyAnalysisContent
    {
        internal static void AutoDownlod(List<string> AllPageList)
        {
            int Allcount = AllPageList.Count;
            int DownLoadCount = 0;
            Console.WriteLine("开始下载");
            foreach (string line in AllPageList)
            {
                Console.WriteLine("总共{0}篇，已下载{1}篇，总进度{2}，当前扫描{3}", Allcount, DownLoadCount++, (float)DownLoadCount / Allcount * 100, line);

                if (BcyStaticValue.UseLogFlag && BcyStaticValue.OldUrlList1.Contains(line))
                {//如果启用日志模式和发现重复则跳出
                    Console.WriteLine("发现重复，不下载");
                    continue;
                }

                string Shtml = WebHelper.HttpPcGet(line);

                int GetScore = TextHelper.StringToInt(
                    HtmlHelper.GetFirstAttributes(Shtml, "data-zan", "//div[@id='js-detailZan']")

                    );
                if (GetScore != 0 && GetScore < BcyStaticValue.Score)
                {
                    Console.WriteLine("评分{0}低于预设{1},跳过。", GetScore, BcyStaticValue.Score);
                    continue;
                }
                else
                    Console.WriteLine("评分{0},预设{1},可下载", GetScore, BcyStaticValue.Score);
                ReadDetailContent(Shtml);
                AddLog(line);
            }
            Console.WriteLine("全部下载完成！");
            if (Helper.StaticValueOld.SaveImgListFlag)
            {
                if (MessageHelper.DialogResultAlert("打开存放路径？"))
                    FloderHelper.OpenFloder(StaticValueOld.SaveListPath);
                if (MessageHelper.DialogResultAlert("需要切割辅助吗？"))
                    WebSiteHelper.SelcetSplit();
            }
        }

        private static void AddLog(string line)
        {
            FileHelper.AppendText(BcyStaticValue.BcyLogPath1, line);
            BcyStaticValue.OldUrlList1.Add(line);
        }

        /// <summary>
        /// 读取小说并且存储
        /// </summary>
        /// <param name="Shtml"></param>
        private static void ReadNovelContent(string Shtml)
        {
            List<string> ImgList = new List<string>();

            string Title = HtmlHelper.GetFirstText(Shtml, "//title");

            String Content = HtmlHelper.GetFirstText(Shtml, "//div[@class='nopaste l-novel-content wb lh2x']");
            if (Title == "")
                Title = "无标题文章";
            Console.WriteLine("标题" + Title);
        }

        /// <summary>
        /// 读取group类型链接
        /// </summary>
        /// <param name="Shtml"></param>
        private static void ReadGroupContent(string Shtml)
        {
            List<string> ImgList = new List<string>();

            string Title = HtmlHelper.GetFirstText(Shtml, "//title") +
                HtmlHelper.GetFirstText(Shtml, "//h1");
            String Content = HtmlHelper.GetFirstText(Shtml, "//div[@class='fz14']");
            if (Title == "")
                Title = "无标题文章";
            Console.WriteLine("标题" + Title);

            Console.WriteLine("内容" + Content);
            foreach (string line in HtmlHelper.GetImgSrcVlaueList(Shtml, "//img[@class='mt20']"))
            {
                ImgList.Add(line.Replace("/w650", ""));
            }
            DownloadHelper(Title, Content, ImgList);
        }

        /// <summary>
        /// 读取cos类型链接  如无意外，他和Illust方法一样
        /// </summary>
        /// <param name="Shtml"></param>
        private static void ReadCoserContent(string Shtml)
        {
            string Title = HtmlHelper.GetFirstText(Shtml, "//title") +
                HtmlHelper.GetFirstText(Shtml, "//h1");
            String Content = HtmlHelper.GetFirstText(Shtml, "//div[@class='post__content js-content-img-wrap js-fullimg js-maincontent mb0 pl0 pr0 l-left w650']");
            int Score = TextHelper.StringToInt(
                HtmlHelper.GetFirstAttributes(Shtml, "data-zan", "//div[@class='btn btn--pink btn--zan btn--base-border-radius ']")
                );
            List<string> ImgList = new List<string>();
            if (Title == "")
                Title = "无标题文章";
            Console.WriteLine("评分数量：" + Score);
            Console.WriteLine("标题" + Title);

            Console.WriteLine("内容" + Content);
            if (Score != 0 && Score < BcyStaticValue.Score)
            {
                Console.WriteLine("评分低于预设值，跳出");
                return;
            }
            foreach (string line in HtmlHelper.GetImgSrcVlaueList(Shtml, "//img[@class='detail_std detail_clickable']"))
            {
                ImgList.Add(line.Replace("/w650", ""));
            }
            DownloadHelper(Title, Content, ImgList);
        }

        /// <summary>
        /// 读取Detail类型链接
        ///
        /// </summary>
        /// <param name="Shtml"></param>
        private static void ReadDetailContent(string Shtml)
        {
            string Title = HtmlHelper.GetFirstText(Shtml, "//title");
            String Content = TextHelper.RegexText(HtmlHelper.GetFirstText(Shtml, "//div[@class='post__content js-content-img-wrap js-fullimg js-maincontent mb0 pl0 pr0 l-left w650']"), "\\s+");
            List<string> ImgList = new List<string>();
            if (Title == "")
                Title = "无标题文章";
            Console.WriteLine("标题" + Title);

            Console.WriteLine("内容" + Content);
            foreach (string line in HtmlHelper.GetImgSrcVlaueList(Shtml, "//img[@class='detail_std detail_clickable']"))
            {
                ImgList.Add(line.Replace("/w650", ""));
            }

            DownloadHelper(Title, Content, ImgList);
        }

        /// <summary>
        /// 选择文件的保存方式
        /// </summary>
        /// <param name="Title"></param>
        /// <param name="Content"></param>
        /// <param name="ImgList"></param>
        private static void DownloadHelper(string Title, string Content, List<string> ImgList)
        {
            if (Helper.StaticValueOld.SaveImgListFlag)
            {
                WebCurrency.SaveHelper.SaveList(ImgList, "半次元", string.Format("标签 {0}", BcyStaticValue.Tag));
            }
            else

                BcyDownload.Start(Title, Content, ImgList);
        }
    }
}